﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using WebMonitor.Http;

namespace WebMonitor {
    public class IndexController : Controller {
        public override IDictionary<string, object> Execute(IHttpContext context) {
            var viewParams = new Dictionary<string, object>();
            using (var proc = Process.GetCurrentProcess()) {
                viewParams["workingSet"] = proc.WorkingSet64;
                viewParams["workingSet_Peak"] = proc.PeakWorkingSet64;
                viewParams["virtualMem"] = proc.VirtualMemorySize64;
                viewParams["virtualMem_Peak"] = proc.PeakVirtualMemorySize64;
                viewParams["pagedMem"] = proc.PagedMemorySize64;
                viewParams["pagedMem_Peak"] = proc.PeakPagedMemorySize64;
                viewParams["startTime"] = proc.StartTime;
                viewParams["upTime"] = DateTime.Now.Subtract(proc.StartTime);
            }
            viewParams["perfCounters_aspnet"] = GetPerfCounters("ASP.NET");
            return viewParams;
        }

        public ICollection GetPerfCounters(string category) {
            var r = new ArrayList();
            var cat = new PerformanceCounterCategory(category);
            foreach (var counter in cat.GetCounters()) {
                r.Add(new DictionaryEntry(counter.CounterName, counter.NextValue()));
            }
            return r;
        }

        public float GetPerfCounter(string category, string counter) {
            using (var c = new PerformanceCounter(category, counter))
                return c.NextValue();
        }
    }
}